package com.blackhorse.config;

import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServletRequest;


/**
 * 定义区分sentinel资源的授权规则的“来源”，测试授权规则时打开注解
 */
//@Configuration
public class RequestOriginParserDefinition implements RequestOriginParser {
    @Override
    public String parseOrigin(HttpServletRequest request) {
        /*
        本质是通过request域获取来源标识，如pc/app，然后交给流控应用进行匹配
         */
        String serviceName = request.getParameter("serviceName");
        if (StringUtils.isEmpty(serviceName)) {
            throw new IllegalArgumentException("serviceName不能为空");
        }
        return serviceName;
    }
}
